package easypal.brilliard.world.brilliards.communication.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import easypal.brilliard.util.TraceUtil;
import easypal.brilliard.world.brilliards.communication.CommunicationAgent;
import easypal.brilliard.world.brilliards.communication.CommunicationReceiver;
import easypal.brilliard.world.brilliards.logic.BallColor;

public class CommunicationReceiverClientImpl implements CommunicationReceiver {

	protected Map<String, String> msg;
	
	protected CommunicationAgent agent;
	protected GameInterfaceSide gameInterface;
	
	public CommunicationReceiverClientImpl() {
	}
	
	public void setGameInterface(GameInterfaceSide gameInterface) {
		this.gameInterface = gameInterface;
	}
	
	@Override
	public synchronized void received(CommunicationAgent agent, String message) {
		//System.out.print(message);
		this.msg = ProtocolUtil.parseMessage(message);
//		System.out.println("-------------  Recived  ----------------------");
//		for (Map.Entry<String, String> e : msg.entrySet()) {
//			TraceUtil.trace(this, e.getKey() + ":" + e.getValue());
//		}
//		System.out.println("-----------------------------------------------");
		if (message.equals("") ) {
			// if the server shutdown
			agent.destory();
			agent = null;
			return;
		}
		
		
		String type = msg.get("type");
		if (type.equals("BALL_ARM")) {
			float angle = Float.parseFloat(msg.get("Angle"));
			this.gameInterface.ballArm(angle);
		} else if (type.equals("BALL_HIT")) {
			int x, y, z, f;
			float dx, dy, dz;
			List<String> list;
			
			list = ProtocolUtil.parseCommaStringToList(msg.get("Offset"));
			x = Integer.parseInt(list.get(0));
			y = Integer.parseInt(list.get(1));
			z = Integer.parseInt(list.get(2));
			
			list = ProtocolUtil.parseCommaStringToList(msg.get("Direction"));
			dx = Float.parseFloat(list.get(0));
			dy = Float.parseFloat(list.get(1));
			dz = Float.parseFloat(list.get(2));
			
			f = Integer.parseInt(msg.get("Force"));
			
			this.gameInterface.ballHit(x, y, z, dx, dy, dz, f);
			
		} else if (type.equals("NEXT_TURN")) {
			String turn = msg.get("Turn");
			String whiteBall = msg.get("WhiteBall");
			int scoreOne = Integer.parseInt(msg.get("ScoreOne"));
			int scoreTwo = Integer.parseInt(msg.get("ScoreTwo"));
			List<String> list = ProtocolUtil.parseCommaStringToList(msg.get("RecoverColorBall"));
			List<BallColor> recoverBalls = new ArrayList<BallColor>(5);
			for (String s : list) {
				recoverBalls.add(BallColor.parseBallColor(s));
			}
			
			this.gameInterface.nextTurn(turn,scoreOne, scoreTwo, whiteBall, recoverBalls);
		}
		else if (type.equals("BEGIN")) {
			this.gameInterface.gameBegins();
		} else if (type.equals("END")){
			this.gameInterface.gameEnds(msg.get("Winner"));
		} else if (type.equals("ONLINE_REQUEST")) {
			this.gameInterface.onlineRequest();
		} else if (type.equals("ANOTHER_OFFLINE")) {
			this.gameInterface.anotherOffline();
			
		} else if (type.equals("WAIT")) {
			this.gameInterface.waitPlayer();
		} else {
			
		}
	}
	
	
	public CommunicationAgent getAgent() {
		return agent;
	}
	public void setAgent(CommunicationAgent agent) {
		this.agent = agent;
	}

}
